package com.lsa.expression.visitor;

import java.util.List;

import com.lsa.expression.tree.ExprNode;
import com.lsa.expression.tree.FactorNode;
import com.lsa.expression.tree.Node;
import com.lsa.expression.tree.OperatorNode;
import com.lsa.expression.tree.TermNode;

public abstract class PrintWalker
{
    public static void walk(Node node, int printOffset)
    {
        if (node.getParent() != null)
        {
            System.out.println();
        }
        
        for( int i = 0; i < printOffset; i++ )
        {
            System.out.print(" ");
        }
        
        if (node instanceof ExprNode)
        {
            System.out.print("Expr");
        }
        else if (node instanceof TermNode)
        {
            System.out.print("Term");
        }
        else if (node instanceof OperatorNode)
        {
            OperatorNode operNode = (OperatorNode)node;
            System.out.print(operNode.getOperator());
        }
        else if (node instanceof FactorNode)
        {
            if (node.getSubNodes().isEmpty())
            {
                FactorNode factorNode = (FactorNode)node;
                System.out.print(factorNode.getData());
            }
            else
            {
                System.out.print("Fact");
            }
        }
        
        List subnodes = node.getSubNodes();        
        for( int i = 0; i < subnodes.size(); i++ )
        {
            walk((Node)subnodes.get(i), printOffset + 1);
        }
    }
}
